package gui.list_objects;

import gui.del_object.Del_Activite;
import gui.edit_objects.Edit_Activite;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import objects.Activite;

import bdd_handler.ActiviteDAO;
import bdd_handler.bdd;

public class List_Activites extends JFrame implements ActionListener, ListSelectionListener, ComponentListener{

	private static final long serialVersionUID = 1L;
	private ActiviteDAO aDAO;
	private Activite[] list_activites;
	private JTable jt_activites;
	private JButton new_activite, edit_activite, del_activite;
	private JFrame parent;

	private result_recherche_activite_model aModel;

	public List_Activites(JFrame parent)
	{
		this.addComponentListener(this);
		//recuperation de la fenetre parente
		this.parent=parent;
		this.parent.setVisible(false);

		//construction de la fenetre
		this.setTitle("Lieux");
		this.setSize(1020,650);
		this.setResizable(false);
		JPanel pan=new JPanel();
		pan.setLayout(null);
		this.setContentPane(pan);

		//creation des DAOs
		bdd db=new bdd();
		aDAO=new ActiviteDAO(db.getConnection());
		
		//creation objets internes
		list_activites=aDAO.findAll();
		aModel=new result_recherche_activite_model(list_activites);
		jt_activites=new JTable(aModel);
		jt_activites.getTableHeader().setReorderingAllowed(false);
		jt_activites.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		jt_activites.getSelectionModel().addListSelectionListener(this);
		
		JScrollPane jsp_activites=new JScrollPane(jt_activites);
		new_activite=new JButton("ajouter activité");
		new_activite.addActionListener(this);
		edit_activite=new JButton("éditer activité");
		edit_activite.addActionListener(this);
		del_activite=new JButton("supprimer activité");
		del_activite.addActionListener(this);
		
		//placer les objets
		pan.add(jsp_activites);
		jsp_activites.setBounds(10, 10, 190, 510);
		pan.add(edit_activite);
		edit_activite.setBounds(10, 530, 190, 20);
		pan.add(del_activite);
		del_activite.setBounds(10, 560, 190, 20);
		pan.add(new_activite);
		new_activite.setBounds(10,590,190,20);
		
		//action fermeture fenetre
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.addWindowListener(
				new WindowAdapter(){
					public void windowClosing(WindowEvent e){
						close();

					}
				}
				);
		//afficher la fenetre
		this.setVisible(true);
	}
	
	private void close()
	{
		this.parent.setVisible(true);
		this.dispose();
	}
	@Override
	public void componentHidden(ComponentEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void componentMoved(ComponentEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void componentResized(ComponentEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void componentShown(ComponentEvent e) {
		list_activites=aDAO.findAll();
		aModel.setNewData(list_activites);
		jt_activites.setModel(aModel);
		jt_activites.revalidate();
		jt_activites.repaint();
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source=e.getSource();
		if(source==edit_activite)
		{
			if(jt_activites.getSelectedRow()<0 || (int)aModel.getValueAt(0, 1)==-1)
			{
				JOptionPane.showMessageDialog(this.getContentPane(), "aucune activité n'est séléctionnée.", "Oh zut!", JOptionPane.ERROR_MESSAGE);
				return;
			}
			if(jt_activites.getSelectedRow()<0 || (int)aModel.getValueAt(jt_activites.getSelectedRow(), 1)==0)
			{
				JOptionPane.showMessageDialog(this.getContentPane(), "cette activité n'est pas modifiable.", "Oh zut!", JOptionPane.ERROR_MESSAGE);
				return;
			}
			
			new Edit_Activite(this,aDAO.find((int)aModel.getValueAt(jt_activites.getSelectedRow(), 1)));
		}
		if(source==del_activite)
		{
			if(jt_activites.getSelectedRow()<0 || (int)aModel.getValueAt(0, 1)==-1)
			{
				JOptionPane.showMessageDialog(this.getContentPane(), "aucune activité n'est séléctionnée.", "Oh zut!", JOptionPane.ERROR_MESSAGE);
				return;
			}
			if(jt_activites.getSelectedRow()<0 || (int)aModel.getValueAt(jt_activites.getSelectedRow(), 1)==0)
			{
				JOptionPane.showMessageDialog(this.getContentPane(), "cette activité ne peut être supprimée.", "Oh zut!", JOptionPane.ERROR_MESSAGE);
				return;
			}
			new Del_Activite(this,aDAO.find((int)aModel.getValueAt(jt_activites.getSelectedRow(), 1)));
		}
		if(source==new_activite)
		{
			new Edit_Activite(this,new Activite(-1, ""));
		}
	}

}
